package ar.org.cfired.creditos.gui.action;

import java.math.BigInteger;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;

import ar.org.cfired.creditos.gui.Sesion;
import ar.org.cfired.creditos.parametros.EstadosLog;
import ar.org.cfired.creditos.parametros.GuardarParametrosException;
import ar.org.cfired.hibernate.utils.HibernateUtil;

public class GuardarModificacionAction extends Action{
	private ExecutionService executionService;
	private ProcessEngine processEngine;
	private Logger objetoLogger = Logger.getLogger(this.getClass().getName());

	@Override
	public void execute(Sesion sesion) throws GuardarParametrosException {
	}

	@Override
	public void execute(String taskID, String usuario, String executionID)
			throws GuardarParametrosException {
		try {
			Map<String, Object> variables = executionService.getVariables(executionID, executionService.getVariableNames(executionID));
			
			String exId = (String)variables.get("executionID");
			objetoLogger.debug(exId);
			if (exId != null) {
				if (variables.containsKey("usuarioModificacion")) variables.remove("usuarioModificacion");
				if (variables.containsKey("executionID")) variables.remove("executionID");
				if (variables.containsKey("decision")) variables.remove("decision");
				objetoLogger.debug("Guardando variables para " + exId);
				executionService.setVariables(exId, variables);
			}
			else {
				throw new GuardarParametrosException();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new GuardarParametrosException();
		}
	}
	
	public void setProcessEngine(ProcessEngine pe) {
		processEngine = pe;
	}
	
	public void setExecutionService(ExecutionService es) {
		executionService = es;
	}
	
	@Override
	public void execute(ExecutionService execution, String taskID, String usuario, String executionID) throws GuardarParametrosException {
		executionService = execution;
		this.execute(taskID, usuario, executionID);
	}

}
